import com.kyub.dynamoscaffold.RowAction
import jofc2.model.elements.LineChart
import jofc2.model.Chart

class PilotController {

    def filterService

    def dynamoScaffold = Pilot

    def listItems = ["surname","name","scores"]

    def searchItems = ["surname"]

    def showItems = ["surname","name","nascarId","scores"]

    def inlines = ["scores"]

    def rowActions = [new RowAction(label:"Extra action", controller: "pilot", action:"rivals", params:["surname"]),new RowAction(url:"/pippo/action/1", label:"Cool action")]

    def secRoles = ["canRead":["ROLE_USER"] , "canInsert" : ["ROLE_ADMIN"] , "canUpdate" : ["ROLE_ADMIN"] ,"canDelete" : ["ROLE_ADMIN"]]

    //TODO secPolicy: una closure per dominarli tutti

  def populate= {
     for(int i = 0; i<20 ; i++){
         Pilot p = new Pilot()
         p.name = i+ " base"
         p.surname = "baz"
         p.nascarId = i*345
         if(p.save(flush:true))
            println("saved " + p.id)
         else
            println("An error occurred " + p.errors)

       }
    render("OK! ")
  }

  def chart = {
       def Pilot pilot = Pilot.get(params.id)

  }

  def chartData = {

       def Pilot p = Pilot.get(params.id)
       def List l = new ArrayList()

       p.scores.each { s ->
            l.add(s.placeNumber)        
       }

       render new Chart("Race results").addElements(new LineChart().addValues(l))

  }


   
}
